var NeihuanliuCfxq = function() {
	var fillDetail = function(id) {
		$.ajax({
			type : "GET",
			cache : false,
			url : "neihuanliu/XQlist?storehousecode=" + id + "&orgcode=" + orgcode,
			success : function(responseJson) {
				if (responseJson.success == true) { // 返回true
					var data = responseJson.obj;
					if (data.length > 0) {
						$("input[name=runmode]").val(data[0].runmode);
						$("input[name=begintime]").val(data[0].begintime);
						$("input[name=endtime]").val(data[0].endtime);
						$("input[name=durationtime]").val(data[0].durationtime);
						$("input[name=power]").val(data[0].power);
						$("input[name=aerationtemp]").val(data[0].aerationtemp);
						$("input[name=aerationhumidity]").val(data[0].aerationhumidity);
						$("input[name=outtemp]").val(data[0].outtemp);
						$("input[name=outhumidity]").val(data[0].outhumidity);
						$("input[name=lowertemp]").val(data[0].lowertemp);
						$("input[name=highttemp]").val(data[0].highttemp);
						$("input[name=avgtemp]").val(data[0].avgtemp);
						$("span[name=intemp]").text(data[0].intemp + "℃");
						$("span[name=inhumidity]").text(data[0].inhumidity);

						//内环流状态
						if (data[0].openstatue == 1) {
							// 如果开启状态							
							$(".neihuanliu-running").hide().next().show();
						} else {
							// 如果关闭状态							
							$(".neihuanliu-running").show().next().hide();
						}
					}
				} else {
					QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "error");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				QTYYComponents.bootstrapSweetAlert("", "查询失败!!!", "error");
			}
		});
	}
	/**
	 * 绑定按钮事件
	 */
	var eventHandler = function() {
		$("#startBtn").on(
			"click",
			function() {
				if (g_intervalId != null) {
					window.clearInterval(g_intervalId);
				}
				var storehousecode = "";
				var id = $("#id").val();
				if (id != null && id != "") {
					storehousecode = id;
				} else {
					storehousecode = $("#cangfangDetail").val();
				}
				if (storehousecode == null || storehousecode == "") {
					alert("请选择仓房");
					return;
				}
				g_intervalId = window.setInterval(function() {
					fillDetail(storehousecode);
				}, 5000);
				QTYYComponents.layerOpenNo("启动", '700px', '300px', "startstop/main?orgcode=" + orgcode
					+ "&storehousecode=" + storehousecode + "&sblx=1&mode=start");
			})
		$("#stopBtn").on(
			"click",
			function() {
				if (g_intervalId != null) {
					window.clearInterval(g_intervalId);
				}
				var storehousecode = "";
				var id = $("#id").val();
				if (id != null && id != "") {
					storehousecode = id;
				} else {
					storehousecode = $("#cangfangDetail").val();
				}
				if (storehousecode == null || storehousecode == "") {
					alert("请选择仓房");
					return;
				}
				g_intervalId = window.setInterval(function() {
					fillDetail(storehousecode);
				}, 5000);
				QTYYComponents.layerOpenNo("停止", '700px', '300px', "startstop/main?orgcode=" + orgcode
					+ "&storehousecode=" + storehousecode + "&sblx=1&mode=stop");
			})
		$(".neihuanliu-running").click(function() {
			var storehousecode = "";
			var id = $("#id").val();
			if (id != null && id != "") {
				storehousecode = id;
			} else {
				storehousecode = $("#cangfangDetail").val();
			}
			if (storehousecode == null || storehousecode == "") {
				alert("请选择仓房");
				return;
			}
			QTYYComponents.saveStartStop(storehousecode, "", 1, 1);
			$(".neihuanliu-running").hide().next().show();
		})
		$(".neihuanliu-running1").click(function() {
			var storehousecode = "";
			var id = $("#id").val();
			if (id != null && id != "") {
				storehousecode = id;
			} else {
				storehousecode = $("#cangfangDetail").val();
			}
			if (storehousecode == null || storehousecode == "") {
				alert("请选择仓房");
				return;
			}
			QTYYComponents.saveStartStop(storehousecode, "", 0, 1);
			$(".neihuanliu-running1").hide().prev().show();
		})
		$("#searchBtn").on("click", function() {
			if (g_intervalId != null) {
				window.clearInterval(g_intervalId);
			}
			var storehousecode = $("#cangfangDetail").val();
			g_intervalId = window.setInterval(function() {
				fillDetail(storehousecode);
			}, 5000);
			$.ajax({
				type : "GET",
				cache : false,
				url : "neihuanliu/LBlist?storehousecode=" + storehousecode + "&orgcode=" + orgcode,
				success : function(responseJson) {
					if (responseJson.success == true) { // 返回true
						var data = responseJson.obj.records;
						if (data.length > 0) {
							$("input[name=runmode]").val(data[0].runmode);
							$("input[name=begintime]").val(data[0].begintime);
							$("input[name=endtime]").val(data[0].endtime);
							$("input[name=durationtime]").val(data[0].durationtime);
							$("input[name=power]").val(data[0].power);
							$("input[name=aerationtemp]").val(data[0].aerationtemp);
							$("input[name=aerationhumidity]").val(data[0].aerationhumidity);
							$("input[name=outtemp]").val(data[0].outtemp);
							$("input[name=outhumidity]").val(data[0].outhumidity);
							$("input[name=lowertemp]").val(data[0].lowertemp);
							$("input[name=highttemp]").val(data[0].highttemp);
							$("input[name=avgtemp]").val(data[0].avgtemp);
							$("span[name=intemp]").text(data[0].intemp + "℃");
							$("span[name=inhumidity]").text(data[0].inhumidity);

							if (data[0].openstatue == 1) {
								$(".neihuanliu-running").hide().next().show();
							} else {
								$(".neihuanliu-running").show().next().hide();
							}
						}
					} else {
						QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "error");
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					QTYYComponents.bootstrapSweetAlert("", "查询失败!!!", "error");
				}
			});
		});

		$("#aiBtn").on(
			"click",
			function() {
				var storehousecode = "";
				var id = $("#id").val();
				if (id != null && id != "") {
					storehousecode = id;
				} else {
					storehousecode = $("#cangfangDetail").val();
				}
				if (aiflag == 0) {
					aiflag = 1;
					QTYYComponents.layerOpen("智能模式", '600px', '300px', "ai/main?orgcode=" + orgcode
						+ "&storehousecode=" + storehousecode + "&sblx=1");
				}
			});
	}

	return {
		init : function() {
			var id = $("#id").val();
			if (id != null && id != "") {
				$("#searchFormDetail").css("display", "none");
				fillDetail(id);
				g_intervalId = window.setInterval(function() {
					fillDetail(id);
				}, 10000);
			}
			QTYYComponents.initSelect("/dict/cangfang", "cangfangDetail");
			eventHandler();
		},
		start : function() {
			$(".neihuanliu-running").hide().next().show();
		},
		stop : function() {
			$(".neihuanliu-running").show().next().hide();
		}
	}
}();

jQuery(document).ready(function() {
	NeihuanliuCfxq.init();
});